package com.example.api.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.api.dto.LessonListRes;
import com.example.api.entity.Course;
import com.example.api.entity.LessonPlan;
import com.example.api.exception.CustomException;
import com.example.api.service.ICourseService;
import com.example.api.service.ILessonPlanService;
import io.jsonwebtoken.io.Decoders;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.example.api.dto.ResStatusCode.COURSE_LESSON_QUERY_ERROR;
import static com.example.api.dto.ResStatusCode.COURSE_QUERY_ERROR;

/**
 * <p>
 * 课时计划 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/api/lesson-plan")
public class LessonPlanController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(LessonPlanController.class);

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ILessonPlanService planService;


    @GetMapping("/{courseId}")
    @ApiOperation("获取课程课时安排列表")
    public ResponseEntity<LessonListRes> getLessons(
            @PathVariable(value = "courseId",required = true)
            @ApiParam("课程ID")
                Long courseId
    ) {
        try {
            Course course = courseService.getById(courseId);
            LessonListRes listRes = new LessonListRes();

            if (course != null) {
                listRes.setCourseHour(course.getCountHour());
                listRes.setCoursePrice(course.getPrice());
                listRes.setCourseId(course.getId());
                listRes.setSaleCount(course.getSaleCount());
                listRes.setIsBuy(isBuyCourse(courseId,getUser()));

                //开始查询所有的课程
                QueryWrapper<LessonPlan> query = new QueryWrapper<>();
                query.eq("course_id",courseId);
                List<LessonPlan> lessonPlans = planService.list(query);
                listRes.setLessons(lessonPlans);

                return ResponseEntity.ok(listRes);
            }else {
                throw new CustomException(courseId + "", COURSE_QUERY_ERROR.getCode(), COURSE_QUERY_ERROR.getMessage());
            }
        } catch (CustomException e) {
            log.error(e.getMessage());
            throw new CustomException(courseId + "", COURSE_LESSON_QUERY_ERROR.getCode(), COURSE_LESSON_QUERY_ERROR.getMessage());

        }

    }

    @GetMapping("/getKey")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<byte[]> getLessonKey(Integer lessonId) {

        //获取课时
        LessonPlan lessonPlan = planService.getById(lessonId);

        if (lessonPlan != null) {

            Boolean bugCourse = isBuyCourse(lessonPlan.getCourseId(), getUser());
            if (bugCourse) {
                //获取一个64位的key
                byte[] decode = Decoders.BASE64.decode(lessonPlan.getVideoKey());
                return ResponseEntity.ok(decode);
            }
        }
        return ResponseEntity.badRequest().build();
    }

}
